package www.openteam.com.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.*;

import java.util.List;

import www.openteam.com.R;
import www.openteam.com.bean.DeliverGoods;

/**
 * Created by kylin on 2016/8/19.
 */
public class RefreshAdapter extends  RecyclerView.Adapter<RecyclerView.ViewHolder> {

    private static final int TYPE_ITEM = 0;
    private static final int TYPE_FOOTER = 1;
    private Context context;
    private List<DeliverGoods.DataEntity> data;
    private final String TAG="RefreshAdapter";
    public interface OnItemClickListener {
        void onItemClick(View view, int position);

        void onItemLongClick(View view, int position);
    }

    private OnItemClickListener onItemClickListener;

    public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
        this.onItemClickListener = onItemClickListener;
    }

    @Override
    public int getItemCount() {
//        Log.d(TAG, "getItemCount: "+data.size());
        return data.size() == 0 ? 0 : data.size() + 1;
    }

    @Override
    public int getItemViewType(int position) {
        if (position + 1 == getItemCount()) {
            return TYPE_FOOTER;
        } else {
            return TYPE_ITEM;
        }
    }

    public RefreshAdapter(Context context, List<DeliverGoods.DataEntity> data) {
        this.context = context;
        this.data = data;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        if (viewType == TYPE_ITEM) {
            View view = LayoutInflater.from(context).inflate(R.layout.item_base, parent,
                    false);

            return new ItemViewHolder(view);
        } else if (viewType == TYPE_FOOTER) {
            View view = LayoutInflater.from(context).inflate(R.layout.item_foot, parent,
                    false);
            return new FootViewHolder(view);
        }
        return null;
    }


    @Override
    public void onBindViewHolder(final RecyclerView.ViewHolder holder, int position) {
        if (holder instanceof ItemViewHolder) {
            ((ItemViewHolder) holder).bindDeliverData(data,position);

            if (onItemClickListener != null) {
                holder.itemView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        int position = holder.getLayoutPosition();
                        onItemClickListener.onItemClick(holder.itemView, position);
                    }
                });

                holder.itemView.setOnLongClickListener(new View.OnLongClickListener() {
                    @Override
                    public boolean onLongClick(View v) {
                        int position = holder.getLayoutPosition();
                        onItemClickListener.onItemLongClick(holder.itemView, position);
                        return false;
                    }
                });
            }
        }
    }


    static class ItemViewHolder extends RecyclerView.ViewHolder {

        TextView tv_id;
        TextView tv_title;

        public ItemViewHolder(View view) {
            super(view);
            tv_id = (TextView) view.findViewById(R.id.tv_id);
            tv_title = (TextView) view.findViewById(R.id.tv_title);

        }


        public void bindDeliverData(List<DeliverGoods.DataEntity> data,int position){
           tv_id.setText("编号:"+data.get(position).getId()+"");
            if (data.get(position).getNotSendNumb().equals(".0000")){
                data.get(position).setNotSendNumb("0.0000");
            }
           tv_title.setText("客户单号:"+data.get(position).getCustorderid() + "\t\t\t" +"客户名称:"+data.get(position).getCustName()
                    + "\t "+"币别:"+ data.get(position).getMoenyType() + "\t\t\t\t"  +"订单日期:" +data.get(position).getSenddate() +
                    "\t" + "产品名称:"+data.get(position).getProdName() + "\t\t\t\t"+"订单数量:" + data.get(position).getPricenumb() +
                    "\t "+"送货地址:"+ data.get(position).getSenddate() + "\t\t\t\t" + "送货数:"+data.get(position).getPaynumb() +
                    "\t" + "未送数量:"+data.get(position).getNotSendNumb() + "\t\t\t\t" +"退货数:"+ data.get(position).getBacknumb()
            );

        }


        public void bindTakeDeliverData(List<DeliverGoods.DataEntity> data,int position){
            tv_id.setText("编号:"+data.get(position).getId()+"");
            if (data.get(position).getNotSendNumb().equals(".0000")){
                data.get(position).setNotSendNumb("0.0000");
            }
            tv_title.setText("客户单号:"+data.get(position).getCustorderid() + "\t\t\t" +"客户名称:"+data.get(position).getCustName()
                    + "\t "+"币别:"+ data.get(position).getMoenyType() + "\t\t\t\t"  +"订单日期:" +data.get(position).getSenddate() +
                    "\t" + "产品名称:"+data.get(position).getProdName() + "\t\t\t\t"+"订单数量:" + data.get(position).getPricenumb() +
                    "\t "+"送货地址:"+ data.get(position).getSenddate() + "\t\t\t\t" + "送货数:"+data.get(position).getPaynumb() +
                    "\t" + "未送数量:"+data.get(position).getNotSendNumb() + "\t\t\t\t" +"退货数:"+ data.get(position).getBacknumb()
            );

        }
    }

    public static class FootViewHolder extends RecyclerView.ViewHolder {

        TextView tv_foot;
        public FootViewHolder(View view) {
            super(view);
            tv_foot = (TextView) view.findViewById(R.id.foot_tv);
        }

        public void setTv_foot(String s) {
            tv_foot.setText(s);
        }
    }
}